<?php namespace Admin\Controller;
class GoodsListController extends CommonController{
	protected $db;
	public function __construct(){
		$this->db = new \Common\Model\GoodsList;
		parent::__construct();
		
	
	}
  public function index(){
  	$gid = 	Q("get.gid",0,'intval');
	$tid = Q("get.tid",0,'intval');

	$typedata  = Db::table("360mall_type_attr")->where("ta_type_id",$tid)->where("class",1)->field("ta_type_id","ta_name","ta_id")->get();
	
foreach ($typedata as $k => $v) {
	$typedata[$k]["select"] = Db::table("360mall_goods_attr")->where("ga_taid",$v['ta_id'])->where("ga_gid",$gid)->field("ga_value","ga_id")->get();
}

    //获得旧数据
    $olddata  = $this->db->where("gl_gid",$gid)->get();
	foreach ($olddata as $k => $v) {
		$olddata[$k]['gl_combine'] = explode(",", $v['gl_combine']);
		foreach ($olddata[$k]['gl_combine']as $kk => $vv) {
			$olddata[$k]['gl_combine'][$kk] = Db::table("360mall_goods_attr")
			->where("ga_id",$vv)->field("ga_id","ga_value")->first();
		}
	}
	//总共几条
	$count = $this->db->count();
	//显示几条
	$page = Page::row(8)->make($count);
    View::with("page",$page);
	View::with("olddata",$olddata);
	View::with("typedata",$typedata);
  	View::make();
  }
//添加货品
	public function add(){
       $gid = 	Q("get.gid",0,'intval');
	    $tid = Q("get.tid",0,'intval');
		if(IS_POST){
			
			if(!$this->db->store()){
				View::error($this->db->getError());
			}else{
				View::success("添加成功",U("index",array("tid"=>$tid,"gid"=>$gid)));
			}
		}
	}
	//编辑
	public function edit(){
		 $gid =Q("get.gid",0,'intval');
	     $tid = Q("get.tid",0,'intval');	
		 $glid = Q("get.glid",0,'intval');
		 	$typedata  = Db::table("360mall_type_attr")->where("ta_type_id",$tid)->where("class",1)->field("ta_type_id","ta_name","ta_id")->get();
foreach ($typedata as $k => $v) {
	$typedata[$k]["select"] = Db::table("360mall_goods_attr")->where("ga_taid",$v['ta_id'])->where("ga_gid",$gid)->field("ga_value","ga_id")->get();
}
//获得旧数据
 $olddata  = $this->db->where("gl_id",$glid)->first();
	
		$olddata['gl_combine'] = explode(",", $olddata['gl_combine']);
		foreach ($olddata['gl_combine']as $k => $v) {
			$olddata['gl_combine'][$k] = Db::table("360mall_goods_attr")
			->where("ga_id",$v)->field("ga_id","ga_value")->first();
		}

    View::with("olddata",$olddata);
	View::with("typedata",$typedata);
	if(IS_POST){
		if(!$this->db->edit($glid)){
				View::error($this->db->getError());
			}else{
				View::success("编辑成功",U("index",array("tid"=>$tid,"gid"=>$gid)));
			}
		}
  	View::make();
	}
	//删除
		 public function del(){
		 $gid =Q("get.gid",0,'intval');
	     $tid = Q("get.tid",0,'intval');	
		 $glid = Q("get.glid",0,'intval');
		 $this->db->where("gl_id",$glid)->delete();
		 View::success("删除成功",U("index",array("tid"=>$tid,"gid"=>$gid)));
		 }
}